package com.xxxx.yoyo.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xxxx.yoyo.bean.Order;
import com.xxxx.yoyo.bean.Type;
import com.xxxx.yoyo.bean.User;
import com.xxxx.yoyo.service.IOrderService;
import com.xxxx.yoyo.service.ITypeService;
import com.xxxx.yoyo.service.impl.OrderServiceImpl;
import com.xxxx.yoyo.service.impl.TypeServiceImpl;

/**
 * 我的订单列表类
 * Servlet implementation class OrderListServlet
 */
@WebServlet("/index/order")
public class OrderListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	ITypeService typeService = new TypeServiceImpl();
	IOrderService orderService = new OrderServiceImpl();
	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//获取session
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		if(user == null) {//说明没有登录,或者session超时失效了
			//重定向到登录页,让用户去登录
			response.sendRedirect("/YoyoShop/index/login");
		}else {
			//设置导航条上的 [我的订单] 处于选中状态
			request.setAttribute("flag", 3);
			
			//获取 [宠物分类] 列表
			List<Type> typeList = typeService.getTypeList();
			//把typeList存入request中,供/index/order.jsp使用
			request.setAttribute("typeList", typeList);
			
			//根据[用户id]获取订单列表
			int userId = user.getId();
			List<Order> orderList = orderService.getOrderListByUserId(userId);
			if(orderList.size() != 0) {//如果查到的订单个数不是0个
				//把订单列表存入request,供/index/order.jsp页面使用
				request.setAttribute("orderList", orderList);
			}
			
			//请求转发,跳转到/index/order.jsp页面
			request.getRequestDispatcher("/index/order.jsp").forward(request, response);
		}
		
		
	}

}
